<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="850px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '添加代理类型' : '修改代理类型'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">

      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
         <el-form-item label="类型名称:" prop="agencyName">
           <el-input
             clearable
             :maxlength="20"
             v-model="form.agencyName"
             placeholder="请输入类型名称"
           />
         </el-form-item>
           <!--<el-form-item label="代理价格:" prop="agencyOffer" class="delInput">
             <el-input
               clearable
               type="number" step="0.01"
               :maxlength="20"
               v-model="form.agencyOffer"
               placeholder="代理价格"
             />
           </el-form-item>-->
           </el-col>
           <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="备注:" prop="remark">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.remark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>
<!--      <prize ref="prize"  @uploadPrice="uploadPrice"></prize>-->
      <el-card shadow="never">
        <div class="flex-between m-bottom20">
          <div class="fit-title">套餐列表</div>
        </div>
        <el-table
          max-height="273px"
          :data="tableData"
          ref="table"
          tooltip-effect="dark"
          border
          stripe>
          <el-table-column  label="套餐名称" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mealName"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="套餐零售价">
            <template slot-scope="scope" class="delInput">
              <div class="delInput">
                <el-input type="number" step="0.01" v-model.number="scope.row.retailPrice"></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="代理拿货价">
            <template slot-scope="scope">
              <div class="delInput">
                <el-input type="number" step="0.01" v-model.number="scope.row.deliveryPrice"></el-input>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import prize from './prize.vue'
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import {agencyAdd, agencyUpdate, checkExistence, mealPageList} from '@/api/oem';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect,prize},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        // 总平台1 oem代理2
        types:1,
        id:'',
        agencyName: '',
        agencyOffer: '',
        remark: '',
        agentMealList: [],
      };
      return {
        // 套餐列表数据
        tableData: [],
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          agencyName: [
            {
              required: true,
              message: '请输入代理名称',
              trigger: 'blur'
            }
          ],
          /*agencyOffer: [
            {
              required: true,
              message: '请输入代理价格',
              trigger: 'blur'
            }
          ],*/
          mealId: [
            {
              required: true,
              message: '请选择关联套餐',
              trigger: 'blur'
            }
          ],
          activityName: [
            {
              required: true,
              message: '请输入活动名称',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
    },
    methods: {
      init(row){
        /* 获取角色数据 */
        let parms ={
          types:1
        }
        if(row){
          parms.agentId=row.id
        }
        else{
          parms={
            types:1
          }
        }
        mealPageList(parms)
          .then((list) => {
            this.tableData = list;
            // this.$emit('uploadPrice',this.tableData)
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /*uploadPrice(e){
        console.log('e===================',e)
        // return
        this.form.agentMealList=e
        // this.confirm()
      },*/
      /* 保存编辑 */
      save() {
        console.log("this.tableData",this.tableData)
        this.form.agentMealList=this.tableData
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
          };
          console.log("this.form=======",this.form)
          const saveOrUpdate = this.isUpdate ? agencyAdd : agencyUpdate;
          if (!this.isUpdate) {
            let encryptor = new JSEncrypt()
            let pk = this.$store.state.signature.pk
            encryptor.setPublicKey(pk)
            data.password = encryptor.encrypt(data.password)
          }
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              let hint = this.isUpdate ? '代理类型添加成功！' : '代理类型修改成功！'
              // this.$parent.mealPageLists()
              this.$message.success(hint);
              for(let i=0;i<this.tableData.length;i++){
                this.tableData[i].deliveryPrice =''
              }
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              let hint = this.isUpdate ? '代理类型添加失败！' : '代理类型修改失败！'
              this.$message.error(hint);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$refs.form.clearValidate();
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            delete this.form.types
            this.$util.assignObject(this.form, {
              ...this.data,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = false;
          } else {
            this.isUpdate = true;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>

<style scoped>
  .fit-title{
    color: #333333;
    font-size: 16px;
    margin:0px 2px;
  }
  /*隐藏input（type=number)右边的小箭头*/
  /deep/.delInput input::-webkit-outer-spin-button,
  /deep/.delInput input::-webkit-inner-spin-button {
    -webkit-appearance: none!important;
  }
  /deep/.delInput input[type="number"]{
    -moz-appearance: textfield;
  }
</style>
